package com.koron.order.inspect.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.koron.bean.query.PageQuery;
import com.koron.common.core.interceptor.OrderByInterceptor;
import com.koron.order.inspect.bean.po.OrderInspectionPo;
import com.koron.order.inspect.bean.query.OrderInspectionQuery;
import com.koron.order.inspect.bean.vo.OrderInspectionVo;
import com.koron.order.inspect.mapper.OrderInspectionMapper;
import com.koron.order.inspect.service.OrderInspectionService;
import org.apache.commons.collections4.CollectionUtils;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 工单抽查意见表(OrderInspection)表服务实现类
 *
 * @author zhouj
 * @since 2022-01-14 10:16:20
 */
@Service("orderInspectionService")
public class OrderInspectionServiceImpl implements OrderInspectionService {

    @Override
    @TaskAnnotation("insertOrUpdate")
    public OrderInspectionPo insertOrUpdate(SessionFactory factory, OrderInspectionPo orderInspection) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        //抽查时 只能允许具有高优先级的抽查权限的人抽查,先找出抽查此工单的最高优先权
        mapper.insertOrUpdate(orderInspection);

        return orderInspection;
    }

    @Override
    @TaskAnnotation("insertOrUpdateBatch")
    public int insertOrUpdateBatch(SessionFactory factory, List<OrderInspectionPo> orderInspectionList) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        return mapper.insertOrUpdateBatch(orderInspectionList);
    }

    @Override
    @TaskAnnotation("insert")
    public int insert(SessionFactory factory, OrderInspectionPo orderInspection) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        return mapper.insert(orderInspection);
    }

    @Override
    @TaskAnnotation("insertBatch")
    public int insertBatch(SessionFactory factory, List<OrderInspectionPo> orderInspectionList) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        return mapper.insertBatch(orderInspectionList);
    }

    @Override
    @TaskAnnotation("update")
    public int update(SessionFactory factory, OrderInspectionPo orderInspection) {
        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);
        return mapper.update(orderInspection);
    }

    @Override
    @TaskAnnotation("deleteById")
    public int deleteById(SessionFactory factory, String id) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);
        return mapper.deleteById(id);
    }

    @Override
    @TaskAnnotation("queryById")
    public OrderInspectionPo queryById(SessionFactory factory, String id) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        return mapper.queryById(id);
    }

    @Override
    @TaskAnnotation("queryList")
    public List<OrderInspectionPo> queryList(SessionFactory factory, OrderInspectionPo orderInspection) {

        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        List<OrderInspectionPo> list = mapper.queryList(orderInspection);

        return list;
    }

    @Override
    @TaskAnnotation("queryInspectPageList")
    public PageInfo<OrderInspectionVo> queryInspectPageList(SessionFactory factory, OrderInspectionQuery inspectionQuery, PageQuery pageQuery) {
        OrderByInterceptor.setOrderBy(pageQuery.getOrders());
        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        List<String> orderStatusList = inspectionQuery.getOrderStatusList();

        //列表只展示状态为已归档或已巡视
        if (CollectionUtils.isEmpty(orderStatusList)) {
            List<String> list = new ArrayList<>();
            list.add("ARCHIVED");
            list.add("END_PATROL");
            inspectionQuery.setOrderStatusList(list);
        }

        List<OrderInspectionVo> list1 = mapper.queryInspectPageList(inspectionQuery);
        List<OrderInspectionVo> list2 = queryInspectPatrolPageList(factory,inspectionQuery,pageQuery);
        list1.addAll(list2);
        PageInfo<OrderInspectionVo> pageInfo = getPageInfo(pageQuery.getPage(), pageQuery.getPageSize(), list1);
        return pageInfo;
    }

    public static <T> PageInfo<T> getPageInfo(int currentPage, int pageSize, List<T> list) {
        int total = list.size();
        if (total > pageSize) {
            int toIndex = pageSize * currentPage;
            if (toIndex > total) {
                toIndex = total;
            }
            if (pageSize * (currentPage - 1) < toIndex) {
                list = list.subList(pageSize * (currentPage - 1), toIndex);
            }
            else {
                list=new ArrayList<>();
            }
        }
        Page<T> page = new Page<>(currentPage, pageSize);
        page.addAll(list);
        page.setPages((total + pageSize - 1) / pageSize);
        page.setTotal(total);

        PageInfo<T> pageInfo = new PageInfo<>(page);
        return pageInfo;
    }

    @TaskAnnotation("queryInspectPatrolPageList")
    public List queryInspectPatrolPageList(SessionFactory factory, OrderInspectionQuery inspectionQuery, PageQuery pageQuery){
        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);
        List<OrderInspectionVo> result = mapper.queryInspectPatrolPageList(inspectionQuery);
        return result;
    }

    @Override
    @TaskAnnotation("queryPageList")
    public PageInfo queryPageList(SessionFactory factory, OrderInspectionPo orderInspection, PageQuery pageQuery) {

        PageHelper.startPage(pageQuery.getPage(), pageQuery.getPageSize(), pageQuery.getOrderBy());
        OrderInspectionMapper mapper = factory.getMapper(OrderInspectionMapper.class);

        List<OrderInspectionPo> list = mapper.queryList(orderInspection);

        return new PageInfo(list);
    }

}